public class Test1 {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
    }

    class BinaryTree {
        private TreeNode root;

        public BinaryTree() {
            this.root = null;
        }

        public void setRoot(TreeNode root) {
            this.root = root;
        }

        // 前序遍历
        public void preOrderTraversal(TreeNode node) {
            if (node != null) {
                System.out.print(node.val + " ");
                preOrderTraversal(node.left);
                preOrderTraversal(node.right);
            }
        }

        // 中序遍历
        public void inOrderTraversal(TreeNode node) {
            if (node != null) {
                inOrderTraversal(node.left);
                System.out.print(node.val + " ");
                inOrderTraversal(node.right);
            }
        }

        // 后序遍历
        public void postOrderTraversal(TreeNode node) {
            if (node != null) {
                postOrderTraversal(node.left);
                postOrderTraversal(node.right);
                System.out.print(node.val + " ");
            }
        }

        public static void main(String[] args) {
            BinaryTree tree = new BinaryTree();

            // 构建一个简单的二叉树
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.right = new TreeNode(3);
            root.left.left = new TreeNode(4);
            root.left.right = new TreeNode(5);

            tree.setRoot(root);

            System.out.print("前序遍历结果: ");
            tree.preOrderTraversal(root);
            System.out.println();

            System.out.print("中序遍历结果: ");
            tree.inOrderTraversal(root);
            System.out.println();

            System.out.print("后序遍历结果: ");
            tree.postOrderTraversal(root);
            System.out.println();
        }
    }
}
